﻿using System;
using System.Linq;
using System.Linq.Dynamic;
using System.Web.Mvc;
using ChoViet.CMS.ViewModels;
using ChoViet.Lib.Common;
using ChoViet.Lib.Helpers;
using ChoViet.Lib.Paging;
using ChoViet.Model;
using System.Collections;
using System.Data;

namespace ChoViet.CMS.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class ReportController : BaseController
    {
        /// <summary>
        /// Created by: VietLv
        /// Created date: 29/06/2012
        /// Description: Manage tags
        /// </summary>

        ChoVietDataContext eDataContext = new ChoVietDataContext();

        #region Report/Total view by day
        //
        // GET: /Report/Total view by day

        public ActionResult IndexCms()
        {
             
            ReportViewModel model = new ReportViewModel();
            DateTime fromdate = Request["Fromdate"] != null && Request["Fromdate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["Fromdate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : DateTime.Now.AddDays(-30);
            DateTime todate = Request["Todate"] != null && Request["Todate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["Todate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : DateTime.Now;
            if (Request["Todate"] != null)
            {
                todate = todate.AddHours(23);
                todate = todate.AddMinutes(59);
            }
            model.Counters = eDataContext.Counters.Where(e => e.AddDate >= fromdate && e.AddDate <= todate).OrderBy("AddDate","ASC").ToList();
            model.Fromdate = fromdate.ToString("dd/MM/yyyy");
            model.Todate = todate.ToString("dd/MM/yyyy");
            model.ReportNewAds = eDataContext.ReportNewAds.Where(e => e.CreatedDate >= fromdate && e.CreatedDate <= todate).OrderBy("CreatedDate", "ASC").ToList();
            
            return View(model);
        }

        public ActionResult UpdateReport()
        {
            var ex = eDataContext.ReportNewAds.Max(e => e.CreatedDate).Value;

            DateTime ndatetime = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day);
            ex = ex.AddDays(-1);
            while (ex <= ndatetime)
            {
             DateTime tem = ex.AddHours(23).AddMinutes(59);
             int newAd = eDataContext.Ads.Where(e => e.ModifyDate >= ex && e.ModifyDate <= tem && e.SourceUrl == null).Count();
             int newAdAuto = eDataContext.Ads.Where(e => e.ModifyDate >= ex && e.ModifyDate <= tem && e.SourceUrl != null).Count();
             var isExit = eDataContext.ReportNewAds.Where(e => e.CreatedDate == ex).FirstOrDefault();
             if (isExit != null && isExit.ID > 0)
             {
                 isExit.NewAd = newAd;
                 isExit.NewAdAuto = newAdAuto;
                 eDataContext.SubmitChanges();
             }
             else
             {
                 ReportNewAd rnewAd = new ReportNewAd();
                 rnewAd.NewAd = newAd;
                 rnewAd.NewAdAuto = newAdAuto;
                 rnewAd.CreatedDate = ex;
                 eDataContext.ReportNewAds.InsertOnSubmit(rnewAd);
             }
             ex = ex.AddDays(1);
            }
            eDataContext.SubmitChanges();
            Messages.AddFlashMessage("Đã cập nhật dữ liệu cho báo cáo thành công!");
                return RedirectToAction("IndexCms");   
        }
        #endregion
    }
}
